/*
* @Date: 2020/3/24
* @Author: XueChengwu <xuechengwu@erayt.com>
* @Copyright: 2015-2019 Erayt, Inc.
* @Description: If you have some questions, please contact: xuechengwu@erayt.com.
*/
import React from 'react';
//import CodeMirror from 'react-codemirror';
import 'codemirror/lib/codemirror.css';
import CodeMirror from 'codemirror/lib/codemirror';
import 'codemirror/mode/jsx/jsx';
import 'codemirror/mode/javascript/javascript';
import './index.scss';
const { remote } = window.require('electron');
const file = remote.require('./server/file');
export default class extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
    this.state = {
      dir: '',
      file: '',
    };
  }

  componentDidMount() {
    this.codeMirror = CodeMirror.fromTextArea(this.myRef.current, {
      mode: 'text/jsx',
      lineNumbers: true,
    });
  }
  onChangeCode(code) {
    console.log(code);
    t
  }

  onNewFile = () => {

    file.newFile('macd', './indicator')
      .then((res) => {
        this.setState({
          ...res
        }, () => {
          console.log(this.state);
        })
      });
  }

  onSaveFile = () => {
    file.saveFile(
      `${this.state.dir}/${this.state.file}`,
      this.codeMirror.getValue()
    ).then(res => {
      console.log('has saved')
    });
  }

  onCompileFile = () => {
    file.compileFile(`${this.state.dir}/${this.state.file}`, this.codeMirror.getValue())
      .then(res => {
        console.log(res);
      }).catch(err => {
        console.log(err);
    })
  }

  onRun = () => {
    file.runFile(`${this.state.dir}/${this.state.file}`)
      .then(res => {
        console.log(res);
      }).catch(err => {
        console.log(err);
    });

  }

  render() {
    return (
      <div className="xue-framework">

        <div className="xue-file-structor">
          <div onClick={this.onNewFile}>新建</div>
          <div onClick={this.onSaveFile}>保存</div>
          <div onClick={this.onCompileFile}>编译</div>
          <div onClick={this.onRun}>运行</div>
        </div>
        <div>
          <textarea ref={this.myRef} className="code-mirror" onChange={this.onChangeCode}></textarea>
        </div>
      </div>

    );
  }
}
